﻿
(function () {

    appModule.controller('tenant.views.manuscriptStatus.index', [
        '$scope', '$uibModal', 'uiGridConstants', 'abp.services.app.manuscriptStatus',
        function ($scope, $modal, uiGridConstants, manuscriptStatusService) {
            var vm = this;

            $scope.$on('$viewContentLoaded', function () {
                Metronic.initAjax();
            });

            vm.loading = false;
            vm.filterText = null;
            vm.curManuscriptStatus = null;
            //获取权限权限
            vm.permissions = {
                create: abp.auth.hasPermission('Pages.Tenant.ManuscriptStatus.Create'),
                edit: abp.auth.hasPermission('Pages.Tenant.ManuscriptStatus.Update'),
                'delete': abp.auth.hasPermission('Pages.Tenant.ManuscriptStatus.Delete')
            };
            
            var urlManuscriptStatus = "/api/services/app/manuscriptStatus/";
            //列表数据源含分页
            var mainGridDataSource = new kendo.data.DataSource({
                type: 'aspnetmvc-ajax',
                transport: {
                    read: {
                        url: urlManuscriptStatus + "KendoGetManuscriptStatuss",
                        contentType: "application/json",
                        type: "POST"
                    },
                    parameterMap: function (options, operation) {
                        return JSON.stringify(options);
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.data;
                        }
                        return {};
                    },
                    total: function (data) {
                        if (data.success === true && data.result != null) {
                            return data.result.total;
                        }
                        return 0;
                    },
                    model: {
                        id: "id",
                        fields: {
                            id: { type: "number" },
                        }
                    }
                }
            });
            vm.mainGridOptions = {
                dataSource: mainGridDataSource,//数据源，类型是json
                columnMenu: true,
                reorderable: true,
                resizable: true,
                filterable: true,
                scrollable: true,
                selectable: "multiple",
                allowCopy: true,
                //height: 410,
                change: function (e) {
                    var selectedRows = this.select();
                    for (var i = 0; i < selectedRows.length; i++) {
                         vm.curManuscriptStatus = this.dataItem(selectedRows[i]);
                    }
                },
                pageable: {
                    buttonCount: 5.0,
                    pageSize: 10,
                    pageSizes: [10, 20, 50, 100],
                    refresh: true
                },
                sortable: {
                    mode: "multiple"
                },
                messages: {
                    commands: {
                        excel: "导出"
                    }
                },
                columns: [ 
                    {
                        title: '稿件状态ID ',
                        field: 'id'
                    },
                    {
                        title: '状态名称 ',
                        field: 'manuscriptStatusName'
                    },
                    {
                        title: '排序 ',
                        field: 'order'
                    },
                    //{
                    //    title: '编号 ',
                    //    field: 'code',
                    //    width: 100
                    //},
                ]
            };
            //打开编辑页面（添加页、修改页）
            function openCreateOreditManuscriptStatusModal(manuscriptStatusId) {
                var modalInstance = $modal.open({
                    templateUrl: '~/App/tenant/views/manuscriptstatus/createOrEditManuscriptStatus.cshtml',
                    controller: 'tenant.views.manuscriptstatus.createOrEditManuscriptStatus as vm',
                    backdrop: 'static',
                    resolve: {
                        manuscriptStatusId: function () {
                            return manuscriptStatusId;
                        }
                    }
                });
                modalInstance.result.then(function (result) {
                    vm.mainGridOptions.dataSource.read();
                });
            }
            //修改
            vm.editManuscriptStatus = function () {
                if (vm.curManuscriptStatus) {
                    openCreateOreditManuscriptStatusModal(vm.curManuscriptStatus.id);
                }else{
                    abp.message.error("请选择要修改的行!");
                }
            };
            //添加
            vm.createManuscriptStatus = function () {
                openCreateOreditManuscriptStatusModal(null);
            };
            //删除
            vm.deleteManuscriptStatus = function () {
                if (vm.curManuscriptStatus != null &&vm.curManuscriptStatus.id > 0) {
                    abp.message.confirm(
                        "是否要删除此条记录！",
                        function (isConfirmed) {
                            if (isConfirmed) {
                                manuscriptStatusService.deleteManuscriptStatus({ id:vm.curManuscriptStatus.id }).success(function () {
                                    abp.notify.info(app.localize('SavedSuccessfully'));
                                    vm.mainGridOptions.dataSource.read();
                                    vm.curManuscriptStatus = null;
                                }).finally(function () {

                                });
                            }
                        }
                    );
                } else {
                    abp.message.error("请选择要删除的行!");
                }
            };

        }]);
})();
